package com.dexilaonian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dexilaonian.R;
import com.dexilaonian.adpter.BindPersonAdapter;
import com.dexilaonian.adpter.PersonAdapter;
import com.dexilaonian.bean.Person_BindBean;
import com.dexilaonian.ui.BaseActivity;
import com.dexilaonian.utils.Constant;
import com.dexilaonian.utils.JsonUtil;
import com.dexilaonian.utils.Okhttp;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class Person_bindActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.base_title_service3)
    TextView baseTitleService3;
    @Bind(R.id.btn_personbind)
    Button btnPersonbind;
    @Bind(R.id.listview_person)
    ListView listviewPerson;
    private List<Person_BindBean> person_list = new ArrayList<>();
    private String realName = "";
    private String BMI_STATUS = "";
    private int customer_id;
    private String bind_content;

    @Override
    public int getLayoutID() {
        return R.layout.activity_person_bind;
    }

    @Override
    public void initView() {
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        baseTitleService3.setText("绑定人员");
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            bind_content = extras.getString("BIND_CONTENT");
        }
        initPerson();
    }

    private void initPerson() {
        showProgressBar(true);
        String bind_url = Constant.base_url + "/health_record/list.do";
        Map<String, String> parm = new HashMap<>();
        Okhttp.postString(true, bind_url, parm, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
                showToast(state);
            }

            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                Log.e("TTTTperson", response);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 16 || status == 34) {
                        startActivity(new Intent(Person_bindActivity.this, LoginActivity.class));
                    }
                    if (status == 0) {
                        String data = jsonObject.optString("data");
                        JSONArray jsonArray = jsonObject.optJSONArray("data");
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                            Person_BindBean bindbean = new Person_BindBean();
                            bindbean.setBMI_STATUS(jsonObject2.optString("BMI_STATUS"));
                            bindbean.setRealName(jsonObject2.optString("realName"));
                            bindbean.setCustomer_id(jsonObject2.optInt("customer_id"));
                            person_list.add(bindbean);
                        }
                 /*     ArrayList<Person_BindBean> person_bindBeen = JsonUtil.json2beans(data, Person_BindBean.class);
                        person_list.addAll(person_bindBeen);*/
                        final BindPersonAdapter personAdapter = new BindPersonAdapter(Person_bindActivity.this, person_list);
                        listviewPerson.setAdapter(personAdapter);
                        customer_id = person_list.get(0).getCustomer_id();
                        listviewPerson.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                personAdapter.addChance(position);
                                personAdapter.notifyDataSetChanged();
                                customer_id = person_list.get(position).getCustomer_id();
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
                showToast(state);
            }
        });
    }

    @OnClick({R.id.back, R.id.btn_personbind})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.btn_personbind:
                getBindSuccess();
                break;
        }
    }

    private void getBindSuccess() {
        showProgressBar(true);
        String bind_url = Constant.base_url + "/Aiqiangua/bind_imei.do";
        Map<String, String> bind_map = new HashMap<>();
        bind_map.put(" customerId", customer_id+"");
        bind_map.put("imei", bind_content);

        Okhttp.postString(true, bind_url, bind_map, new Okhttp.CallBac() {
            @Override
            public void onError(Call call, Exception e, String state, int id) {
                showProgressBar(false);
                showToast(state);
            }

            @Override
            public void onResponse(String response, int id) {
                showProgressBar(false);
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    int status = Integer.parseInt(jsonObject.optString("status"));
                    if (status == 16 || status == 34) {
                        startActivity(new Intent(Person_bindActivity.this, LoginActivity.class));
                    }
                    if (status == 44) {
                        showToast("该手表设备已被绑定");
                    }
                    if (status == 0) {
                        startActivity(new Intent(Person_bindActivity.this,BindSuccessActivity.class));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onNoNetwork(String state) {
                showProgressBar(false);
                showToast(state);
            }
        });
    }
}
